package com.tkjt.pms.modules.report.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkIndexValueAttrs;
import com.tkjt.pms.modules.common.mapper.TkIndexValueAttrsMapper;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.entity.TkIndexValueMirror;
import com.tkjt.pms.modules.report.service.ExcelBuilder;
import com.tkjt.pms.modules.report.service.IndexDataLoader;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderParam;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderResult;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderUtils;
import com.tkjt.pms.modules.report.util.DeptConstant;
import com.tkjt.pms.modules.report.vo.DatagridVO;

@Service("myReport")
public class MyReportExcelBuilder implements ExcelBuilder {
	
	@Autowired
	private IndexDataLoader indexDataLoader;
	@Autowired
	private TkIndexValueAttrsMapper tkIndexValueAttrsMapper;

	@SuppressWarnings("unchecked")
	@Override
	public ExcelBuilderResult doExcelBuilder(ExcelBuilderParam excelBuilderParam) {

		ExcelBuilderResult result = new ExcelBuilderResult();
		if(excelBuilderParam.getParam(ExcelBuilderParam.DETAIL_CONDITIONS) == null || ((Map<String, String>)excelBuilderParam.getParam(ExcelBuilderParam.DETAIL_CONDITIONS)).get("conditions") != null){
			// 列定义
			List<ExcelExportEntity> excelColumns = this.buildExcelTitle(excelBuilderParam);
			result.setExcelColumns(excelColumns);
		}
		if(excelBuilderParam.getParam(ExcelBuilderParam.DETAIL_CONDITIONS) == null || ((Map<String, String>)excelBuilderParam.getParam(ExcelBuilderParam.DETAIL_CONDITIONS)).get("detail") != null){
			// 数据组组建
			List<Map<String, Object>> excelCellValues = this.buildExcelCellValues(excelBuilderParam);
			result.setExcelCellValues(excelCellValues);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private List<ExcelExportEntity> buildExcelTitle(ExcelBuilderParam excelBuilderParam) {

		Map<String, String> titleNames = (Map<String, String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_NAMES);
		String indexYearMonth = (String)excelBuilderParam.getParam(ExcelBuilderParam.INDEX_YEAR_MONTH);
		List<ExcelExportEntity> excelColumns = new ArrayList<ExcelExportEntity>();

		// 第一部分:指标名称
		{
			ExcelExportEntity indexColumn = new ExcelExportEntity("指标", "indexCodeColumn");
			indexColumn.setNeedMerge(true);
			excelColumns.add(indexColumn);
		}
		// 第二部分:机构权重系系列
		{
			List<String> weightDeparts = null;
			weightDeparts = (List<String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_WEIGHT_DEPARTS);
			for (String wDepart : weightDeparts) {
				ExcelExportEntity weightDepartColumn = new ExcelExportEntity(titleNames.get(wDepart), ExcelBuilderUtils.buildWeightKey(wDepart));
				weightDepartColumn.setNeedMerge(true);
				excelColumns.add(weightDepartColumn);
			}

		}
		// 第三部分: 考核项(指标值)
		{
			ExcelExportEntity indexValueColumns = new ExcelExportEntity("指标值", "indexValueColumns");
			indexValueColumns.setWidth(21);
			List<ExcelExportEntity> indexValueColumnsSubItems = new ArrayList<>();
			ExcelExportEntity indexValueColumnName = new ExcelExportEntity("考核项", "indexValueColumnsName");
			indexValueColumnName.setWidth(21);
			indexValueColumnsSubItems.add(indexValueColumnName);
			indexValueColumns.setList(indexValueColumnsSubItems);
			excelColumns.add(indexValueColumns);
		}
		// 第四部分: 考核机构
		{
			List<String> dataDeparts = null;
			dataDeparts = (List<String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_DATA_DEPARTS);
			String month = indexYearMonth.substring(4);
			month = month.startsWith("0") ? month.substring(1) : month;
			for (String departCode : dataDeparts) {

				ExcelExportEntity departColumns = new ExcelExportEntity(titleNames.get(departCode), "v" + departCode);

				List<ExcelExportEntity> departColumnsSubItems = new ArrayList<>();
				List<String> columnKeys = ExcelBuilderUtils.getColumnsMKeys();
				for(String key : columnKeys) {
					ExcelExportEntity entity = new ExcelExportEntity(ExcelBuilderUtils.getDataName(key), key);
					departColumnsSubItems.add(entity);
				}
				departColumns.setList(departColumnsSubItems);

				excelColumns.add(departColumns);
			}
		}
		return excelColumns;
	}

	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> buildExcelCellValues(ExcelBuilderParam excelBuilderParam) {

		List<Map<String, Object>> excelCellValues = new ArrayList<>();

		String indexYearMonth = (String)excelBuilderParam.getParam(ExcelBuilderParam.INDEX_YEAR_MONTH);
		DatagridVO datagridVO = (DatagridVO)excelBuilderParam.getParam(ExcelBuilderParam.DATAGRIDVO);
		boolean isTotal = (boolean)excelBuilderParam.getParam(ExcelBuilderParam.IS_TOTAL);
		String indexGroup = datagridVO.getIndexGroup();

		//加载要考核的一线单位指标数据
		Map<String, List<TkIndexInfo>> indexs = indexDataLoader.loadIndexs(datagridVO,isTotal);
		String dataType = (String) excelBuilderParam.getParam(ExcelBuilderParam.DATA_TYPE);
		for(String indexCode : indexs.keySet()) {

			Map<String, Object> oneLineValues = new HashMap<>();

			String indexName = indexs.get(indexCode).get(0).getIndexName();

			// 第一部分:指标名称
			{
				oneLineValues.put("indexCodeColumn", indexName);
			}
			// 第二部分:机构权重系系列
			{
				List<String> weightDeparts = (List<String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_WEIGHT_DEPARTS);
				Map<String, String> indexWeights = indexDataLoader.loadIndexWeights(indexYearMonth, indexGroup, indexCode, weightDeparts);
				for(String wDepartCode : weightDeparts) {
					oneLineValues.put(ExcelBuilderUtils.buildWeightKey(wDepartCode), indexWeights.get(wDepartCode) != null ? indexWeights.get(wDepartCode) : "-");
				}

			}
			// 第三部分: 考核项(指标值)
			{
				List<Map<String, Object>> indexValueColumns = new ArrayList<>();

				List<TkIndexInfo> indexInfoList = indexs.get(indexCode);
				for(TkIndexInfo info : indexInfoList) {

					Map<String, Object> indexValueColumnsSubItems = new HashMap<>();
					indexValueColumnsSubItems.put("indexValueColumnsName", info.getIndexValueName());

					indexValueColumns.add(indexValueColumnsSubItems);
				}

				oneLineValues.put("indexValueColumns", indexValueColumns);
			}
			// 第四部分: 考核机构 (要考虑一个部门两个被评分人情况)
			{
				List<String> dataDeparts = null;
				dataDeparts = (List<String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_DATA_DEPARTS);
				Map<String, String> titleNames = (Map<String, String>) excelBuilderParam.getParam(ExcelBuilderParam.TITLE_NAMES);
				for (String dataDepartCode : dataDeparts) {
					List<Map<String, Object>> departColumns = new ArrayList<>();

					String indexDepart = ExcelBuilderUtils.getIndexDepart(dataDepartCode);
					String indexScorer = dataDepartCode.contains("@")?ExcelBuilderUtils.getIndexScorer(dataDepartCode):null;
					List<TkIndexInfo> indexInfoList = indexs.get(indexCode);
					for(TkIndexInfo info : indexInfoList) {
						String indexValueCode = info.getIndexValueCode();
						QueryWrapper<TkIndexValueAttrs> attrQueryWrapper = new QueryWrapper<>();
						attrQueryWrapper.lambda().eq(TkIndexValueAttrs::getIndexValueCode,indexValueCode);
						attrQueryWrapper.lambda().eq(TkIndexValueAttrs::getAttrCode,"indexFormat");
						TkIndexValueAttrs tkIndexValueAttrs = tkIndexValueAttrsMapper.selectOne(attrQueryWrapper);
						String indexFormat = tkIndexValueAttrs.getAttrValue();
						String titleName = titleNames.get(dataDepartCode);
						Map<String, Object> departColumnsSubItems = indexDataLoader.loadMirroIndexValues(indexFormat,indexYearMonth,indexValueCode, indexDepart, indexScorer,titleName,dataType);
						departColumns.add(departColumnsSubItems);
					}
					oneLineValues.put("v"+dataDepartCode, departColumns);
				}
			}
			excelCellValues.add(oneLineValues);
		}

		return excelCellValues;

	}

	public Map<String, Object> setMonth(List<TkIndexValueMirror> list) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (list != null) {
			for (TkIndexValueMirror entity : list) {
				String month = entity.getMirrorCode().substring(6);
				String indexValueCode = entity.getIndexValueCode();
				QueryWrapper<TkIndexValueAttrs> attrQueryWrapper = new QueryWrapper<>();
				attrQueryWrapper.lambda().eq(TkIndexValueAttrs::getIndexValueCode,indexValueCode);
				attrQueryWrapper.lambda().eq(TkIndexValueAttrs::getAttrCode,"indexFormat");
				TkIndexValueAttrs tkIndexValueAttrs = tkIndexValueAttrsMapper.selectOne(attrQueryWrapper);
				String indexFormat = tkIndexValueAttrs.getAttrValue();
				switch (month) {
					case "01":
						map.put("m01", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "02":
						map.put("m02", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "03":
						map.put("m03", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "04":
						map.put("m04", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "05":
						map.put("m05", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "06":
						map.put("m06", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "07":
						map.put("m07", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "08":
						map.put("m08", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "09":
						map.put("m09", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "10":
						map.put("m10", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "11":
						map.put("m11", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
					case "12":
						map.put("m12", DeptConstant.getPercent(entity.getMTotal(), indexFormat));
						break;
				}
			}
		} else {
			map.put("m01", "-");
			map.put("m02", "-");
			map.put("m03", "-");
			map.put("m04", "-");
			map.put("m05", "-");
			map.put("m06", "-");
			map.put("m07", "-");
			map.put("m08", "-");
			map.put("m09", "-");
			map.put("m10", "-");
			map.put("m11", "-");
			map.put("m12", "-");
		}
		return map;
	}
}
